﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using FikriKibar.Business.Abstract;
using FikriKibar.Business.Concrete;
using FikriKibar.Common.Entities;
using FikriKibar.Common.Helpers;
using FikriKibar.UI.WebCommon.Security;

namespace FikriKibar.UI.WebManagement
{
    public class MvcApplication : HttpApplication
    {
        protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            var path = Request.RawUrl;
            var pathItemList = path.Split('/').ToList();
            var language = pathItemList[1];
            var defaultLanguage = ConfigurationManager.AppSettings["DefaultLanguage"];
            if (language == "")
            {
                Response.Redirect("/" + defaultLanguage);
            }
            else
            {
                ILanguageService languageManager = new LanguageManager();
                var languageCodeList = languageManager.GetAllLanguageCodes();
                if (languageCodeList.Contains(language))
                {
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
                    Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(language);
                }
                else
                {
                    Response.Redirect("/" + defaultLanguage);
                }
            }
        }

        protected void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
        {
            var webSecurityManager = new WebSecurityManager();
            var fikriKibarPrinciple = webSecurityManager.Get(Constants.PrivateCookieName);

            if (fikriKibarPrinciple == null)
            {
                var defaultLanguage = ConfigurationHelper.GetAppSetting("DefaultLanguage");
                var roles = new List<int> { 0 };
                var fikriKibarIdentity = new FikriKibarIdentity
                {
                    UserId = 0,
                    IsAuthenticated = false,
                    Name = "Guest",
                    Username = "Guest",
                    RememberMe = false,
                    LanguageCode = defaultLanguage,
                    UserRoles = roles,
                    FirstName = "",
                    LastName = ""

                };
                fikriKibarPrinciple = new FikriKibarPrincipal(fikriKibarIdentity);
            }
            Thread.CurrentPrincipal = fikriKibarPrinciple;
            HttpContext.Current.User = fikriKibarPrinciple;

        }

        public override void Init()
        {
            PostAuthenticateRequest += MvcApplication_PostAuthenticateRequest;
            base.Init();
        }

        protected void Application_EndRequest()
        {
            if (Context.Response.StatusCode == 404)
            {
                Response.Redirect("/");
            }
        }


        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            BundleTable.EnableOptimizations = false;
        }
    }
}
